<?php

namespace App\Http\Controllers\Api;

use App\OrderGoods;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class OrderGoodsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * @param OrderGoods $orderGood
     * @return OrderGoods
     */
    public function show(OrderGoods $orderGood)
    {
        return $orderGood;
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\OrderGoods  $orderGoods
     * @return \Illuminate\Http\Response
     */
    public function edit(OrderGoods $orderGoods)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\OrderGoods  $orderGoods
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, OrderGoods $orderGoods)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\OrderGoods  $orderGoods
     * @return \Illuminate\Http\Response
     */
    public function destroy(OrderGoods $orderGoods)
    {
        //
    }
    public function userOrderGoods(Request $request)
    {
        $action = $request->get('action');
        $orderGoods = OrderGoods::where('user_id', Auth::id())->whereHas('order', function($query) {
            return $query->where('order_status', 'FINISHED');
        });
        if ($action == 'comment') {
            $orderGoods = $orderGoods->whereNull('commented_at');
        }
        $orderGoods = $orderGoods->latest()->limit(30)->get();
        return $orderGoods;
    }
}
